package cn.edu.zju.om.data;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Index;

@Entity
@Table(name = "ReviewTarget")
@org.hibernate.annotations.Table(appliesTo="ReviewTarget", indexes = {
		@Index(name = "BARCODE_IDX", columnNames={"ean13Code"})})
public class ReviewTarget {
	

	private String targetId;
	
	private String name;
	
	private String category;
	
	private String ean13Code;
	
	private String url;
	
	private String appendix;
	
	private Set<Comment> comments;

	public ReviewTarget(String name, String category, String ean13Code, String url, String appendix) {
		this.name = name;
		this.category = category;
		this.ean13Code = ean13Code;
		this.url = url;
		this.appendix = appendix;
		this.comments = new HashSet<Comment>();
	}

	ReviewTarget() {
	}
	
	@Id
	@GenericGenerator(name = "unique_id", strategy = "uuid")
	@GeneratedValue(generator = "unique_id")
	public String getTargetId() {
		return targetId;
	}

	public void setTargetId(String id) {
		this.targetId = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}


	public String getEan13Code() {
		return ean13Code;
	}

	public void setEan13Code(String ean13Code) {
		this.ean13Code = ean13Code;
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Lob
	public String getAppendix() {
		return appendix;
	}

	public void setAppendix(String appendix) {
		this.appendix = appendix;
	}

	@OneToMany(cascade = CascadeType.ALL)
	@JoinTable(
			name = "ReviewTargetComment",
			joinColumns = { @JoinColumn( name = "targetId") },
			inverseJoinColumns = @JoinColumn( name = "commentId")
	)
	public Set<Comment> getComments() {
		return comments;
	}
	
	public synchronized void addComment(Comment comment) {
		comments.add(comment);
	}

	public void setComments(Set<Comment> comments) {
		this.comments = comments;
	}
	
	

}
